//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BSpline.h"
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CBSpline::CBSpline (UInt32 p, UInt32 n, Cephei::IVector<Double>^ knots) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoVector<Double>^ _Cknots;
    try
    {
#ifdef HANDLE
        _phBSpline = NULL;
#endif
        QuantLib::Natural _p = (QuantLib::Natural)ValueHelper::Convert (p);
        QuantLib::Natural _n = (QuantLib::Natural)ValueHelper::Convert (n);
        CoVector<Double>^ _Cknots = safe_cast<CoVector<Double>^> (knots);
        _Cknots->Lock();
        INativeVector<Double>^ _NCIknots = _Cknots->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCknots = safe_cast<CDoubleVector^>(_NCIknots);
        std::vector<QuantLib::Real>& _knots = static_cast<std::vector<QuantLib::Real>&> (_NCknots->GetReference ());
        _ppBSpline = new boost::shared_ptr<QuantLib::BSpline> (new QuantLib::BSpline ( _p,  _n,  _knots ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cknots != nullptr) _Cknots->Unlock();
    }
}
Cephei::QL::Math::CBSpline::CBSpline (boost::shared_ptr<QuantLib::BSpline>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBSpline = NULL;
#endif
	_ppBSpline = &childNative;
    
}
Cephei::QL::Math::CBSpline::CBSpline (QuantLib::BSpline& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBSpline = NULL;
#endif
	_ppBSpline = new boost::shared_ptr<QuantLib::BSpline> (&childNative);
    
    _BSplineOwner = owner;
    
}

Cephei::QL::Math::CBSpline::CBSpline (CBSpline^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBSpline = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBSpline = new boost::shared_ptr<QuantLib::BSpline> (copy->GetShared());
        
    }
}
Cephei::QL::Math::CBSpline::CBSpline (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBSpline = NULL;
#endif
	if (!t->IsSubclassOf(CBSpline::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CBSpline::CBSpline (QuantLib::Handle<QuantLib::BSpline>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBSpline = &childNative;
	_ppBSpline = &static_cast<boost::shared_ptr<QuantLib::BSpline>>(childNative.currentLink());
    
    _BSplineOwner = owner;
}
Cephei::QL::Math::CBSpline::CBSpline (QuantLib::Handle<QuantLib::BSpline> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBSpline = &childNative;
	_ppBSpline = &static_cast<boost::shared_ptr<QuantLib::BSpline>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CBSpline::CBSpline (QuantLib::BSpline childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBSpline = NULL;
#endif
	_ppBSpline = new boost::shared_ptr<QuantLib::BSpline> (new QuantLib::BSpline (childNative));
    
}
#endif

Cephei::QL::Math::CBSpline::~CBSpline ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBSpline != NULL)
    {
	    delete _ppBSpline;
        _ppBSpline = NULL;
    }
}
Cephei::QL::Math::CBSpline::!CBSpline ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBSpline != NULL)
    {
	    delete _ppBSpline;
    }
}
QuantLib::BSpline& Cephei::QL::Math::CBSpline::GetReference ()
{
    if (_ppBSpline == NULL) throw gcnew NativeNullException ();
	return **_ppBSpline;
}
boost::shared_ptr<QuantLib::BSpline>& Cephei::QL::Math::CBSpline::GetShared ()
{
    if (_ppBSpline == NULL) throw gcnew NativeNullException ();
	return *_ppBSpline;
}
QuantLib::BSpline* Cephei::QL::Math::CBSpline::GetPointer ()
{
    if (_ppBSpline == NULL) throw gcnew NativeNullException ();
	return &**_ppBSpline;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BSpline>& Cephei::QL::Math::CBSpline::GetHandle ()
{
	if (_phBSpline == NULL)
	{
		_phBSpline = new Handle<QuantLib::BSpline> (*_ppBSpline);
	}
	return *_phBSpline;
}
#endif
bool Cephei::QL::Math::CBSpline::HasNative () 
{
	return (_ppBSpline != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::IBSpline^ Cephei::QL::Math::CBSpline_Factory::Create (UInt32 p, UInt32 n, Cephei::IVector<Double>^ knots)
{
    return gcnew CBSpline ( p,  n,  knots);
}
